/**
 * @author NGUYEN Van Tho
 */

package ifi.p17.view;

import ifi.p17.Constant;
import ifi.p17.Person;
import ifi.p17.model.Model;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;

import ifi.p17.controler.Control;


public class ViewListPerson extends JPanel implements ActionListener{

	private static final String LABEL_NEW = "Add new person";
	private static final String LABEL_CANCEL = "Annuler";
    //private Model model;
	private	JTable		table;
	private	JScrollPane scrollPane;
	private Control control;
	private	String		columnNames[];
	private	String		dataValues[][];
	//DataModel dataModel = new DataModel();
    
    public ViewListPerson(Control control) {
        super(new BorderLayout());
        //this.model = model;        
        //model.addObserver(new ModelObserver());
//        createCollumns();
//        getData();
        this.setController(control);
        
        //dataModel.setData(control.search(""));
        table = new JTable(control.getModel());
        
        //table.setDefaultRenderer(Value.class, new ValueRenderer());
        //table.setDefaultEditor(Value.class, new ValueEditor());
        //table.setAutoCreateRowSorter(true);        
        table.setRowHeight(24);
        scrollPane = new JScrollPane(table);
//        this.add(table.getTableHeader(), BorderLayout.NORTH);
//        this.add(table, BorderLayout.CENTER);
        add(scrollPane);
        JPanel panelButton = new JPanel();
        JButton buttonNew = new JButton(LABEL_NEW);
        JButton buttonCancel = new JButton(LABEL_CANCEL);
        buttonNew.addActionListener(this);
        buttonCancel.addActionListener(this);
        panelButton.add(buttonNew);
        panelButton.add(buttonCancel);
        this.add(panelButton, BorderLayout.SOUTH);
        //table.set
    }

    @Override
    public void actionPerformed(ActionEvent e) {
    	String command = e.getActionCommand();
    	if (LABEL_NEW.equals(command)) {
    		//control.changeView(MainView.LIST_VIEW);
    		control.changeToDetailAndClear();
    	}
    	if (LABEL_CANCEL.equals(command)) {
    		SwingUtilities.getWindowAncestor(this).setVisible(false);
    		SwingUtilities.getWindowAncestor(this).dispose();
    	}
//    	else{
//    		Person person = new Person();
//    		person.setName("test");
//    		person.setHomeTelephone("97987");
//    		person.setWorkTelephone("97987");
//    		person.setWorkAddress("xxx");
//    		person.setHomeAddress("xxx");
//    		control.insertPerson(person);
//    		control.getModel().fireTableDataChanged();    		
//    		this.tableRepaint();
//    	}
    }
        
    
    public void setController(Control control){
    	this.control = control;
    }
    public void tableRepaint(){
    	table.revalidate();
    	table.repaint();
    }
    private void createCollumns(){
    	columnNames = new String[] {Constant.NAME, Constant.HOME_PHONE, Constant.WORK_PHONE, Constant.HOME_ADDRESS, Constant.WORK_ADDRESS};
    }
    
    private void getData(){
    	dataValues = new String[5][];
    	dataValues[0] = new String[] {"Tho", "0915083133", "0915083144", "HN", "HCM"};
    }
    private class ModelObserver implements Observer {

        @Override
        public void update(Observable o, Object arg) {
        	
        }
    }

    private static class DataModel extends AbstractTableModel {

        //private static final int MAX = 8;
        private static final String[] names = {Constant.NAME, Constant.HOME_PHONE, Constant.WORK_PHONE, Constant.HOME_ADDRESS, Constant.WORK_ADDRESS};
        private List<Person> persons = new ArrayList<Person>();

        public DataModel() {
        	
        }
        public void setData(List<Person> persons){
        	this.persons = persons;
        }
        public void insertPerson(Person person){
        	this.persons.add(person);
        	fireTableDataChanged();
        }
        @Override
        public int getRowCount() {
            return persons.size();
        }

        @Override
        public int getColumnCount() {
            return names.length;
        }

        @Override
        public Object getValueAt(int row, int col) {
        	Person person = (Person) persons.get(row);
        	switch (col) {
			case 0:
				return person.getName();
			case 1:
				return person.getHomeTelephone();
			case 2:
				return person.getWorkTelephone();
			case 3:
				return person.getHomeAddress();
			case 4:
				return person.getHomeTelephone();
				
			default:
				return "";
			} 
        }

        @Override
        public void setValueAt(Object aValue, int row, int col) {
            if (col == 1) {
                //persons.get(row).set= (Boolean) aValue;
                this.fireTableCellUpdated(row, col);
                
            }
        }

        @Override
        public Class<?> getColumnClass(int col) {
        	 return getValueAt(0,col).getClass();
        }

        @Override
        public String getColumnName(int col) {
            return names[col];
        }

        @Override
        public boolean isCellEditable(int row, int col) {
            return col == 1;
        }
    }    

/*    private static class PersonRenderer extends 
        implements TableCellRenderer {

        private static final Color hilite = new Color(0xE8E8E8);

        public PersonRenderer() {
        	//this.setOpaque(true);
			// TODO Auto-generated constructor stub
		}

        @Override
        public Component getTableCellRendererComponent(
            JTable table, Object person, boolean isSelected,
            boolean hasFocus, int row, int col) {
        	Person p = (Person) person;
            this.setSelected(p.selected);
            //this.setText(df.format(v.value));
            if (isSelected) {
                this.setBackground(hilite);
            } else {
                this.setBackground(Color.white);
            }
            return this;
        }
    }    
*/    
}